// Package global 全局变量
package global

import (
	"organoid_public/infrastructure/authentication"
	"organoid_public/infrastructure/cache"
	"organoid_public/infrastructure/config"
	"organoid_public/infrastructure/config/model"
	"organoid_public/infrastructure/db/gormclient"
	eventbus "organoid_public/infrastructure/event"
	"organoid_public/pkg/logger"
	"organoid_public/pkg/response"
)

const (
	AppName authentication.AppName = "organoid_public_db"
)

var (
	EventManager *eventbus.EventManager    //事件管理器
	GormDb       *gormclient.DbClient      // GormDb gorm客户端
	Cache        cache.Cache               // Cache 缓存客户端
	AppConfig    *model.App                // AppConfig 应用配置
	GinConfig    *config.Config            // 配置文件
	Log          logger.Logger             // Log 日志记录
	Response     *response.GinRestResponse // Response 请求响应器
)

func InitGlobal(conf *config.Config) {
	AppConfig = conf.App
	GinConfig = conf
	log := initLogger()
	initResponseHandler(log)
	initDb(conf, log)
	initEventBus(log)
}

func initResponseHandler(log logger.Logger) {
	Response = response.NewGinRestResponse(log)
}

func initLogger() logger.Logger {
	l, err := logger.NewZapLogger("./logs")
	if err != nil {
		panic(err)
	}
	Log = l
	return Log
}

func initDb(conf *config.Config, log logger.Logger) {
	GormDb = gormclient.InitDbClient(conf.Db, log)
}

func initEventBus(log logger.Logger) {
	//初始化事件管理器
	EventManager = eventbus.NewEventManager(log)
}
